package com.rural.bus.driver;

import java.util.ArrayList;
import java.util.List;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.view.View.OnClickListener;

import com.rural.bus.driver.adapter.AllMyOrderAdapter;
import com.rural.bus.driver.config.BusConstant;
import com.rural.bus.driver.config.MyPreferenceManager;
import com.rural.bus.driver.config.PreferenceKey;
import com.rural.bus.driver.model.Order;
import com.rural.bus.driver.net.MyHttpUtil;
import com.rural.bus.driver.net.OnNetCallback;
import com.rural.bus.driver.net.Url;
import com.rural.bus.driver.view.xlist.XListView;
import com.rural.bus.driver.view.xlist.XListView.IXListViewListener;

public class OrderListActivity extends BaseActivity implements IXListViewListener{
	
	private XListView listview;
	private List<Order> mOrders = new ArrayList<Order>();
	private AllMyOrderAdapter mAdapter;

	private String userId;
	private int mStartIndex;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_all_my_order);

		initView();
		setListeners();
		initData();
		
	}

	@Override
	protected void initView() {
		mBtMiddle.setText("全部订单");
		mBtMiddle.setVisibility(View.VISIBLE);
		mBtMiddle.setTextColor(getResources().getColor(R.color.white));

		mImgLeft.setVisibility(View.VISIBLE);
		mImgLeft.setBackgroundResource(R.drawable.back_bg_selector);
		
		listview=(XListView) findViewById(R.id.listview);
		listview.setPullRefreshEnable(true);
		listview.setPullLoadEnable(true);
		listview.setXListViewListener(this);

		mAdapter = new AllMyOrderAdapter(this, mOrders);
		listview.setAdapter(mAdapter);
	}
	
	@Override
	protected void initData() {
		userId = MyApplication.driverInfo.drivId;

		mStartIndex = 0;
		getMyOrderList();
	}
	
	private void getMyOrderList() {
		if (TextUtils.isEmpty(userId)) {
			return;
		}
		MyHttpUtil myHttpUtil = new MyHttpUtil();
		myHttpUtil.addBodyParm("udid", userId);
		myHttpUtil.addBodyParm("type", "1");
		myHttpUtil.addBodyParm("start", "" + mStartIndex);
		myHttpUtil.addBodyParm("end", "" + BusConstant.PAGE_SIZE);
		myHttpUtil.send(Url.orderlist_url, new OnNetCallback() {

			@Override
			public void success(JSONObject object) {
				listview.stopRefresh();
				listview.stopLoadMore();
				try {
					if (object.get("status").equals("0")) {
						JSONArray array = object.getJSONArray("result");
						if (mStartIndex == 0) {
							mOrders.clear();
						}
						if (mStartIndex != 0 && array.length() == 0) {
							toast("别拉了，没订单了");
						} else {
							int i = 0;
							while (i < array.length()) {
								Order order = Order.parse(array.getJSONObject(i));
								mOrders.add(order);
								i++;
							}
							mAdapter.notifyDataSetChanged();
						}
						
					} else {
						toast(R.string.data_error);
					}
				} catch (JSONException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			}

			@Override
			public void fail(String error) {
				listview.stopRefresh();
				listview.stopLoadMore();
			}
		});
	}

	@Override
	protected void setListeners() {
		mImgLeft.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View arg0) {
				OrderListActivity.this.finish();
			}
		});
	}

	@Override
	public void onRefresh() {
		mStartIndex = 0;
		getMyOrderList();
	}

	@Override
	public void onLoadMore() {
		mStartIndex += BusConstant.PAGE_SIZE + 1;
		getMyOrderList();
	}

}
